package cn.vawn.base.controller;

import cn.vawn.base.consts.AppConsts;
import cn.vawn.base.consts.Contants;
import cn.vawn.base.dto.ResponseResult;
import cn.vawn.base.entity.AdminUser;
import cn.vawn.base.service.IAdminUserService;
import cn.vawn.base.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private IAdminUserService adminUserService;

    @RequestMapping("doLogin")
    public @ResponseBody
    ResponseResult login(HttpServletRequest request, HttpServletResponse response) {

        ResponseResult msg = new ResponseResult();
        String name = getPara(request, "username");
        String password = getPara(request, "password");
        logger.info("try do login by username: " + name);
        if (StringUtil.notEmpty(name) && StringUtil.notEmpty(password)){
            //TODO 查询admin账号是否正确
            AdminUser adminUser = adminUserService.searchByUsername(name);
            if (adminUser == null){
                msg.setState(-1);
                msg.setMessage("用户名或密码不正确");
            }else {
                String dbPassword = adminUser.getPassword();
                boolean matches = new BCryptPasswordEncoder().matches(password, dbPassword);
                if (matches){
                    request.getSession().setAttribute(Contants.LOGIN_USER_SESSION_KEY,adminUser);
                    msg.setMessage(adminUser.getIsSuperadmin().toString());
                    msg.setState(1);
                }else {
                    msg.setState(-1);
                    msg.setMessage("用户名或密码不正确");
                }
            }
        }else {
            msg.setState(-1);
            msg.setMessage("用户名或密码不能为空");
        }
        return msg;
    }

    @RequestMapping(value = "/doLogout", method = RequestMethod.GET)
    public @ResponseBody
    ResponseResult logout(HttpServletRequest request, HttpServletResponse response) {
        logger.info("try to logout");
        ResponseResult msg = new ResponseResult();
        try {
            request.getSession().invalidate();
            msg.setState(AppConsts.RESPONSE_STATUS_SUCCESS);
            msg.setMessage("退出成功");
        } catch (Exception ex) {
            msg.setState(AppConsts.RESPONSE_STATUS_ERROR);
            msg.setMessage("退出失败");
        }
        return msg;
    }

    @RequestMapping("loadUserInfo")
    public @ResponseBody
    ResponseResult loadUserInfo(HttpServletRequest request, HttpServletResponse response) {
        ResponseResult msg = new ResponseResult();
        AdminUser adminUser = (AdminUser) request.getSession().getAttribute(Contants.LOGIN_USER_SESSION_KEY);
        msg.setData(adminUser.getUsername());
        msg.setState(1);
        return msg;
    }




}